<?php

include dirname(dirname(dirname(__FILE__))) . '/include/common.php';

class webServiceSendEmailGuestListByEvent extends core {

    public function process() {
        $event_id = $this->db->escape(trim($_POST['event_id']));
        $user_id = intval($_POST['user_id']);
        $data = $this->db->get_row("SELECT * FROM events WHERE event_id=$event_id AND `user_id`='$user_id'");
        if ($data) {
            $subject = $data->title . " Guest List";
            $messages = '';
            $total_guest_maybe = $this->db->get_row("SELECT count(guest_id) as total FROM guest_event WHERE event_id=$event_id AND `attend`='0'");
            $total_guest_going = $this->db->get_row("SELECT count(guest_id) as total FROM guest_event WHERE event_id=$event_id AND `attend`='1'");
            $total_guest_not_going = $this->db->get_row("SELECT count(guest_id) as total FROM guest_event WHERE event_id=$event_id AND `attend`='2'");
            //going
            if ($total_guest_going->total > 0) {
                $messages .= '<b>Going:</b>';
                $data_guest_going = $this->db->get_results("SELECT g.firstname, g.lastname, g.city, g.state, g.zipcode, g.address FROM guests  g, guest_event  ge WHERE g.guest_id=ge.guest_id AND ge.event_id={$event_id} AND ge.attend = 1 ORDER BY g.firstname asc ;");

                foreach ($data_guest_going as $guest_going) {
                    $city_state_zipcode = '';
                    if ($guest_going->city !== "") {
                        $city_state_zipcode.=$guest_going->city;
                    }
                    if ($guest_going->state !== "") {
                        if ($city_state_zipcode == "") {
                            $city_state_zipcode.=$guest_going->state;
                        } else {
                            $city_state_zipcode.=' - ' . $guest_going->state;
                        }
                    }
                    if ($guest_going->zipcode !== "") {
                        if ($city_state_zipcode == "") {
                            $city_state_zipcode.=$guest_going->zipcode;
                        } else {
                            $city_state_zipcode.=' - ' . $guest_going->zipcode;
                        }
                    }
                    $address='';
                    if ($guest_going->address != "" || $city_state_zipcode != "") {
                        $address.='<table style="font-family:Arial,Sans-serif;font-size: 14px;" cellspacing="0" cellpadding="0" border="0" >';

                        if ($guest_going->address != "") {
                            $address.='<tr><td valign="top" >' . $guest_going->address . '</td></tr>';
                        }
                        if ($city_state_zipcode != "") {
                            $address.='<tr><td valign="top" ><span><span>' . $city_state_zipcode . '</span></span></td></tr>';
                        }
                        $address.='</table>';
                    }
                    $messages .='<table cellspacing="0" cellpadding="8" border="0"  style="width:100%;font-family:Arial,Sans-serif;font-size:14px;border-width:1px 2px 2px 1px;" ><tr><td><div style="padding:2px">' . $guest_going->firstname . ' ' . $guest_going->lastname . $address.'</div></td></tr></table>';
                    
                }
            }
            //maybe

            if ($total_guest_maybe->total > 0) {
                $messages .= '<b>Maybe:</b>';
                $data_guest_maybe = $this->db->get_results("SELECT g.firstname, g.lastname, g.city, g.state, g.zipcode, g.address FROM guests  g, guest_event  ge WHERE g.guest_id=ge.guest_id AND ge.event_id={$event_id} AND ge.attend = 0 ORDER BY g.firstname asc ;");
                foreach ($data_guest_maybe as $guest_maybe) {
                    $city_state_zipcode = '';
                    if ($guest_maybe->city !== "") {
                        $city_state_zipcode.=$guest_maybe->city;
                    }
                    if ($guest_maybe->state !== "") {
                        if ($city_state_zipcode == "") {
                            $city_state_zipcode.=$guest_maybe->state;
                        } else {
                            $city_state_zipcode.=' - ' . $guest_maybe->state;
                        }
                    }
                    if ($guest_maybe->zipcode !== "") {
                        if ($city_state_zipcode == "") {
                            $city_state_zipcode.=$guest_maybe->zipcode;
                        } else {
                            $city_state_zipcode.=' - ' . $guest_maybe->zipcode;
                        }
                    }
                    $address='';
                    if ($guest_maybe->address != "" || $city_state_zipcode != "") {
                        $address.='<table style="font-family:Arial,Sans-serif;font-size: 14px;" cellspacing="0" cellpadding="0" border="0" >';

                        if ($guest_maybe->address != "") {
                            $address.='<tr><td valign="top" >' . $guest_maybe->address . '</td></tr>';
                        }
                        if ($city_state_zipcode != "") {
                            $address.='<tr><td valign="top" ><span><span>' . $city_state_zipcode . '</span></span></td></tr>';
                        }
                        $address.='</table>';
                    }
                    $messages .='<table cellspacing="0" cellpadding="8" border="0"  style="width:100%;font-size:14px;font-family:Arial,Sans-serif;border-width:1px 2px 2px 1px;" ><tr><td><div style="padding:2px">' . $guest_maybe->firstname . ' ' . $guest_maybe->lastname . $address.'</div></td></tr></table>';
                    
                }
            }
            //not going
            if ($total_guest_not_going->total > 0) {
                $messages .= '<b>Not going:</b>';
                $data_guest_not_going = $this->db->get_results("SELECT g.firstname, g.lastname, g.city, g.state, g.zipcode, g.address FROM guests  g, guest_event  ge WHERE g.guest_id=ge.guest_id AND ge.event_id={$event_id} AND ge.attend = 2 ORDER BY g.firstname asc LIMIT 0,2;");
                foreach ($data_guest_not_going as $guest_not_going) {
                    $city_state_zipcode = '';
                    if ($guest_not_going->city !== "") {
                        $city_state_zipcode.=$guest_not_going->city;
                    }
                    if ($guest_not_going->state !== "") {
                        if ($city_state_zipcode == "") {
                            $city_state_zipcode.=$guest_not_going->state;
                        } else {
                            $city_state_zipcode.=' - ' . $guest_not_going->state;
                        }
                    }
                    if ($guest_not_going->zipcode !== "") {
                        if ($city_state_zipcode == "") {
                            $city_state_zipcode.=$guest_not_going->zipcode;
                        } else {
                            $city_state_zipcode.=' - ' . $guest_not_going->zipcode;
                        }
                    }
                    $address='';
                    if ($guest_not_going->address != "" || $city_state_zipcode != "") {
                        $address.='<table style="font-family:Arial,Sans-serif;font-size: 14px;" cellspacing="0" cellpadding="0" border="0" >';

                        if ($guest_not_going->address != "") {
                            $address.='<tr><td valign="top" >' . $guest_not_going->address . '</td></tr>';
                        }
                        if ($city_state_zipcode != "") {
                            $address.='<tr><td valign="top" ><span><span>' . $city_state_zipcode . '</span></span></td></tr>';
                        }
                        $address.='</table>';
                    }
                    $messages .='<table cellspacing="0" cellpadding="8" border="0"  style="width:100%;font-size:14px;font-family:Arial,Sans-serif;border-width:1px 2px 2px 1px;" ><tr><td><div style="padding:2px">' . $guest_not_going->firstname . ' ' . $guest_not_going->lastname .$address.'</div></td></tr></table>';
                    
                }
            }
            $data_user = $this->db->get_row("SELECT * FROM users WHERE `id`='$user_id'");
            if ($data_user) {
                if (core::send_mail($data_user->email, $subject, $messages)) {
                    $this->_JSONResponse(array('msg' => $data->title . " guest list has been sent"));
                } else {
                    $this->_JSONResponse(array('msg' => "Oops, something went wrong. Please try again"));
                }
            } else {
                $this->_JSONResponse(array('msg' => "User does not exist!"));
            }
        } else {
            $this->_JSONResponse(array('error' => 1, 'msg' => "The event does not exist! Please try again."));
        }
    }

}

$processor = new webServiceSendEmailGuestListByEvent();
$processor->process();
?>